package kiratest;

import okhttp3.*;
import org.junit.Test;

import java.io.File;
import java.io.IOException;

/**
 * Created by wangning on 2016/6/29.
 */

/*
post方式提交文件
 */
public class Posting_a_file {
    public static final MediaType MEDIA_TYPE_MARKDOWN
            = MediaType.parse("text/x-markdown; charset=utf-8");

    private final OkHttpClient client = new OkHttpClient();

    @Test
    public void run() throws Exception {
        File file = new File("C:\\Users\\Administrator\\Desktop\\tmp\\me.sql");

        Request request = new Request.Builder()
                .url("http://localhost:8080/upload/1")
                .post(RequestBody.create(MEDIA_TYPE_MARKDOWN, file))
                .build();

        Response response = client.newCall(request).execute();
        if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

        System.out.println(response.body().string());
    }
}
